﻿Imports Oracle.DataAccess.Client
Public Class cDescripcionDetalle

    Sub New(ByVal idDocumento As String, ByVal numero As Integer, ByVal idProducto As String)

    End Sub
    Public Property idDocumento() As String
        Get

        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property numero() As Integer
        Get

        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Property numeroLinea() As Integer
        Get

        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Property imei() As String
        Get

        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property iccid() As String
        Get

        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property idProducto() As String
        Get

        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property iDetalleInventario() As cDetalleInventarios
        Get

        End Get
        Set(ByVal value As cDetalleInventarios)

        End Set
    End Property

    Protected Overrides Sub Finalize()

    End Sub
End Class
Public Class cCargarDescripcionDtalle
    Private lDsMov As dsMovimiento
    Private lBodega As String
    Private lProducto As String
    Private lCantidad As Integer
    Private lDocumento As String
    Private lNumlinea As Integer
    Private lNumero As Integer
    Public Sub New(ByVal dsMovi As dsMovimiento, ByVal bodega As String, ByVal documento As String, ByVal numlinea As Integer, ByVal numero As Integer, ByVal PosReg As Int32)
        Try
            Dim nReg As Int32 = dsMovi.MOV_DESCRIPCIONDET.Rows.Count - 1
            Dim regDetMov As dsMovimiento.MOV_MOVIMIENTODETALLERow = dsMovi.MOV_MOVIMIENTODETALLE.Rows(PosReg)
            Select Case regDetMov.RowState
                Case DataRowState.Added
                    lCantidad = cGeneral.NZ(regDetMov("CANTIDAD", DataRowVersion.Current))
                    lProducto = regDetMov("IDPRODUCTO", DataRowVersion.Current)
                Case DataRowState.Deleted
                    lProducto = regDetMov("IDPRODUCTO", DataRowVersion.Original)
                    lCantidad = 0
                Case DataRowState.Modified
                    If regDetMov("IDPRODUCTO", DataRowVersion.Current) <> regDetMov("IDPRODUCTO", DataRowVersion.Original) Then
                        For Each drDescDet As dsMovimiento.MOV_DESCRIPCIONDETRow In regDetMov.GetChildRows("FK_MOV_MOVIMIENTODETALLE_MOV_DESCRIPCIONDETALLE", DataRowVersion.Original)
                            drDescDet.Delete()
                        Next
                        lCantidad = regDetMov("CANTIDAD", DataRowVersion.Current)
                    Else
                        lCantidad = regDetMov("CANTIDAD", DataRowVersion.Current) - regDetMov("CANTIDAD", DataRowVersion.Original)
                    End If
                    lProducto = regDetMov("IDPRODUCTO", DataRowVersion.Current)
                    If lCantidad < 0 Then
                        Dim rowsDescDet As dsMovimiento.MOV_DESCRIPCIONDETRow() = regDetMov.GetChildRows("FK_MOV_MOVIMIENTODETALLE_MOV_DESCRIPCIONDETALLE", DataRowVersion.Original)
                        For i As Int32 = (rowsDescDet.Count) + lCantidad To rowsDescDet.Count - 1
                            rowsDescDet(i).Delete()
                        Next
                    End If
                Case Else
                    lCantidad = 0
            End Select
            lDsMov = dsMovi
            lBodega = bodega
            lDocumento = documento
            lNumlinea = numlinea
            lNumero = numero
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try
    End Sub
    Public Sub cargaDescripcionDtalle()
        If lCantidad > 0 Then
            Dim Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Dim cmd As OracleCommand = Conn.CreateCommand
            Try
                Dim entro As Integer = 0
                Dim pSelect As String = "SELECT ROWNUM LINEADESCDET,IDPRODUCTO,IMEI,ICCID,CONSECUTIVO "
                Dim pFrom As String = " FROM INV_DETALLEINVENTARIOS "
                Dim pWhere As String = " WHERE "
                Dim pOrder As String = " ORDER BY CONSECUTIVO,FECHAINGRESO"
                'cmd.Parameters.Add(":PDOCUMENTO", OracleDbType.Varchar2).Value = lDocumento
                'cmd.Parameters.Add(":PNUMLINEA", OracleDbType.Int32).Value = lNumlinea
                'cmd.Parameters.Add(":PNUMERO", OracleDbType.Int32).Value = lNumero
                If lBodega.ToString.Length > 0 Then
                    pWhere = pWhere & " IDBODEGA=:PBODEGA "
                    cmd.Parameters.Add("PBODEGA", OracleDbType.Varchar2).Value = lBodega
                    entro = 1
                End If
                If lProducto.ToString.Length > 0 Then
                    If entro = 1 Then
                        pWhere &= " AND "
                    End If
                    pWhere &= " IDPRODUCTO=:PIDPRODUCTO "
                    cmd.Parameters.Add("PIDPRODUCTO", OracleDbType.Varchar2).Value = lProducto
                    entro = 1
                End If

                If lCantidad > 0 Then
                    If entro = 1 Then
                        pWhere &= " AND "
                    End If
                    pWhere &= " ROWNUM <=:PCANTIDAD "
                    cmd.Parameters.Add("PCANTIDAD", OracleDbType.Int32).Value = lCantidad
                End If
                cmd.CommandText = pSelect & pFrom & pWhere
                Conn.Open()
                Dim dt As New Data.DataTable
                dt.Load(cmd.ExecuteReader)
                For Each dr In dt.Rows
                    Dim drDescDtalle As dsMovimiento.MOV_DESCRIPCIONDETRow
                    drDescDtalle = lDsMov.MOV_DESCRIPCIONDET.NewRow
                    drDescDtalle("IDDOCUMENTO") = lDocumento
                    drDescDtalle("NUMERO") = lNumero
                    drDescDtalle("NUMEROLINEA") = lNumlinea
                    drDescDtalle("IDPRODUCTO") = dr("IDPRODUCTO")
                    drDescDtalle("IMEI") = dr("IMEI")
                    drDescDtalle("ICCID") = dr("ICCID")
                    drDescDtalle("LINEADESCDET") = dr("CONSECUTIVO")
                    lDsMov.MOV_DESCRIPCIONDET.Rows.Add(drDescDtalle)
                Next
            Catch ex As Exception
                cError.SalvaError(ex, "Samuel")
                Throw ex
            Finally
                Conn.Close()
            End Try
        End If
    End Sub
End Class

Public Class cEliminarDescDet
    Private lbDsmov As dsMovimiento
    Private lbProducto As String
    Private lbNumeroLinea As Integer
    Public Sub New(ByVal dsMov As dsMovimiento, ByVal producto As String, ByVal numerolinea As Integer)
        lbDsmov = dsMov
        lbProducto = producto
        lbNumeroLinea = numerolinea
    End Sub

    Public Sub EliminarRegistros()
        Dim numReg As Integer = lbDsmov.MOV_DESCRIPCIONDET.Count - 1
        Try
            While 0 < numReg
                If lbProducto = lbDsmov.MOV_DESCRIPCIONDET.Rows(numReg).Item(3) And lbNumeroLinea = lbDsmov.MOV_DESCRIPCIONDET.Rows(numReg).Item(2) Then
                    lbDsmov.MOV_DESCRIPCIONDET.Rows(numReg).Delete()
                End If

                numReg -= 1
            End While
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

End Class